﻿using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace BeatingMole
{
    /// <summary>
    /// Nguyễn Đặng Anh Duy
    /// MSSV : 11100204
    /// Email : itc.anhduy@gmail.com
    /// <summary>
    class CBusMenu
    {
        #region "Variable"
        CInfoMenu _infoMenu;
        ContentManager _Content;
        Vector2 PosMenu;
        bool IsEnable = true;
        MouseState MouseCur;
        MouseState MousePre;
        #endregion
        #region "Bus"
        CBusCharacter BusCharacter;
        bool _isEnableBusCharacter = false;

        CBusSettings BusSettings;
        bool _isEnableBusSettings = false;

        CBusExit BusExit;
        bool _isEnableBusExit = false;

        CBusAbout BusAbout;
        bool _isEnableBusAbout = false;
        #endregion
        #region "Properties"
        public bool IsEnableBusAbout
        {
            set { _isEnableBusAbout = value; }
            get { return _isEnableBusAbout; }
        }
        public bool IsEnableBusExit
        {
            set { _isEnableBusExit = value; }
            get { return _isEnableBusExit; }
        }
        public bool IsEnableBusSettings
        {
            set { _isEnableBusSettings = value; }
            get { return _isEnableBusSettings; }
        }
        public bool IsEnableBusCharacter
        {
            set { _isEnableBusCharacter = value; }
            get { return _isEnableBusCharacter; }
        }
        public CInfoMenu InfoMenu
        {
            set { _infoMenu = value; }
            get { return _infoMenu;}
        }
        #endregion
        #region "Constructor"
        public CBusMenu()
        {

        }
        /// <summary>
        /// Phương thức khởi tạo của Bus Menu
        /// </summary>
        /// <param name="content">ContentManager quán lý Resource</param>
        public CBusMenu(ContentManager content)
        {
            _Content = content;
            InfoMenu = new CInfoMenu(_Content);
            BusCharacter = new CBusCharacter(_Content);
            BusSettings = new CBusSettings(_Content);
            BusExit = new CBusExit(_Content);
            BusAbout = new CBusAbout(_Content);
            if (CGlobalVariable.IsMenu == true)
            {
                MediaPlayer.IsRepeating = true;
                MediaPlayer.Play(InfoMenu.SongMenu);
            }
            PosMenu = Vector2.Zero;
        }
        #endregion
        #region "Method"
        /// <summary>
        /// Hàm cập nhật Menu
        /// </summary>
        /// <param name="gameTime"></param>
        public void UpdateMenu(GameTime gameTime)
        {
            if (CGlobalVariable.IsEnableMenu == true)
            {
                MouseCur = Mouse.GetState();
                if (MouseCur.LeftButton == ButtonState.Pressed && MousePre.LeftButton == ButtonState.Released)
                {
                    if (new Rectangle(Mouse.GetState().X, Mouse.GetState().Y, 1, 1).Intersects(InfoMenu.RecBtnNewGame))
                    {
                        CGlobalVariable.PosMenuBack = CGlobalVariable.PositionMenu.Down;
                        IsEnableBusCharacter = true;
                        BusCharacter.IsEnableBusMenu = false;
                        CGlobalVariable.IsEnableMenu = false;  
                    }

                    if (new Rectangle(Mouse.GetState().X, Mouse.GetState().Y, 1, 1).Intersects(InfoMenu.RecBtnSettings))
                    {
                        CGlobalVariable.PosMenuBack = CGlobalVariable.PositionMenu.Right;
                        IsEnableBusSettings = true;
                        BusSettings.IsEnableBusMenu = false;
                        CGlobalVariable.IsEnableMenu = false;  
                    }

                    if (new Rectangle(Mouse.GetState().X, Mouse.GetState().Y, 1, 1).Intersects(InfoMenu.RecBtnAbout))
                    {
                        CGlobalVariable.PosMenuBack = CGlobalVariable.PositionMenu.Left;
                        IsEnableBusAbout = true;
                        BusAbout.IsEnableBusMenu = false;
                        CGlobalVariable.IsEnableMenu = false;
                    }

                    if (new Rectangle(Mouse.GetState().X, Mouse.GetState().Y, 1, 1).Intersects(InfoMenu.RecBtnExit))
                    {
                        CGlobalVariable.PosMenuBack = CGlobalVariable.PositionMenu.Top;
                        IsEnableBusExit = true;
                        BusExit.IsEnableBusMenu = false;
                        CGlobalVariable.IsEnableMenu = false;
                    }
                }
                MousePre = MouseCur;
            }
            if (IsEnableBusCharacter == true)
            {
                BusCharacter.Update(gameTime);
            }
            if (IsEnableBusSettings == true)
            {
                BusSettings.Update(gameTime);
            }
            if (IsEnableBusExit == true)
            {
                BusExit.UpdateExit(gameTime);
            }
            if(IsEnableBusAbout == true)
            {
                BusAbout.Update(gameTime);
            }
            GoBack();
        }
        private void GoBack()
        {
            #region "Go"
            if (CGlobalVariable.PosMenuBack == CGlobalVariable.PositionMenu.Top)
            {
                PosMenu.Y -= CGlobalVariable.SpeedTranslate;
                if (PosMenu.Y <= -480)
                {
                    PosMenu.Y = -480;
                }
            }
            if (CGlobalVariable.PosMenuBack == CGlobalVariable.PositionMenu.Down)
            {
                PosMenu.Y += CGlobalVariable.SpeedTranslate;
                if (PosMenu.Y >= 480)
                {
                    PosMenu.Y = 480;
                }
            }
            if (CGlobalVariable.PosMenuBack == CGlobalVariable.PositionMenu.Left)
            {
                PosMenu.X -= CGlobalVariable.SpeedTranslate;
                if (PosMenu.X <= -800)
                {
                    PosMenu.X = -800;
                }
            }
            if (CGlobalVariable.PosMenuBack == CGlobalVariable.PositionMenu.Right)
            {
                PosMenu.X += CGlobalVariable.SpeedTranslate;
                if (PosMenu.X >= 800)
                {
                    PosMenu.X = 800;
                }
            }
            #endregion
            #region "Back"
            if (CGlobalVariable.PosMenuBack == CGlobalVariable.PositionMenu.TopBack)
            {
                PosMenu.Y += CGlobalVariable.SpeedTranslate;
                if (PosMenu.Y >= 0)
                {
                    PosMenu.Y = 0;
                }
            }
            if (CGlobalVariable.PosMenuBack == CGlobalVariable.PositionMenu.DownBack)
            {
                PosMenu.Y -= CGlobalVariable.SpeedTranslate;
                if (PosMenu.Y <= 0)
                {
                    PosMenu.Y = 0;
                }
            }
            if (CGlobalVariable.PosMenuBack == CGlobalVariable.PositionMenu.LeftBack)
            {
                PosMenu.X += CGlobalVariable.SpeedTranslate;
                if (PosMenu.X >= 0)
                {
                    PosMenu.X = 0;
                }
            }
            if (CGlobalVariable.PosMenuBack == CGlobalVariable.PositionMenu.RightBack)
            {
                PosMenu.X -= CGlobalVariable.SpeedTranslate;
                if (PosMenu.X <= 0)
                {
                    PosMenu.X = 0;
                }
            }
            #endregion
        } 
        /// <summary>
        /// Hàm Draw Menu
        /// </summary>
        /// <param name="spriteBatch"></param>
        public void DrawMenu(SpriteBatch spriteBatch)
        {
            if (IsEnableBusCharacter == true)
            {
                BusCharacter.Draw(spriteBatch);
            }
            if (IsEnableBusSettings == true)
            {
                BusSettings.Draw(spriteBatch);
            }
            if (IsEnableBusExit == true)
            {
                BusExit.DrawExit(spriteBatch);
            }
            if (IsEnableBusAbout == true)
            {
                BusAbout.Draw(spriteBatch);
            }
            if (IsEnable == true)
            {
                spriteBatch.Draw(InfoMenu.BgMenu, PosMenu, null, Color.White , 0f, Vector2.Zero, 1f, SpriteEffects.None, 1);
                spriteBatch.Draw(InfoMenu.BgBlack, PosMenu, Color.Black * CGlobalVariable.Opacity);
                spriteBatch.Draw(InfoMenu.BtnNewGame, new Vector2(275 + PosMenu.X, 50 + PosMenu.Y), Color.White);
                spriteBatch.Draw(InfoMenu.BtnSettings, new Vector2(275 + PosMenu.X, 140 + PosMenu.Y), Color.White);
                spriteBatch.Draw(InfoMenu.BtnAbout, new Vector2(275 + PosMenu.X, 230 + PosMenu.Y), Color.White);
                spriteBatch.Draw(InfoMenu.BtnExit, new Vector2(275 + PosMenu.X, 320 + PosMenu.Y), Color.White);
            }
        }
        #endregion
    }
}
